<?php
/**
*	模块信息
*/
namespace Rainrock\Framework\kernel\core;

use Rainrock\Framework\kernel\base\Rock;
use Rainrock\Framework\kernel\base\CLog;
use Rainrock\Framework\kernel\db\DB;


trait ModuleInfo_Update{

	
	/**
	*	$str  转化为 数组 0|昨天,2|d
	*/
	public function strtoarray($str)
	{
		$a	= explode(',', $str);
		$arr= array();
		foreach($a as $a1){
			$a2	= explode('|', $a1);
			$k 	= $a2[0];
			$v 	= $a2[0];
			$c	= '';
			if(isset($a2[1]))$k = $a2[1];
			if(isset($a2[2]))$c = $a2[2];
			$arr[] = array('name'=>$k, 'value'=>$v, 'color'=>$c);
		}
		return $arr;
	}
	
	/**
	*	获取数据
	*/
	public function getData($dstr)
	{
		if(!$dstr)return array();
		if(is_array($dstr))return $dstr;
		$data = array();
		$acta = explode(',', $dstr);
		$act  = $acta[0];
		if(Rock::contain($act, ':')){
			$actb = explode(':', $act);
			$obj  = Model::get($actb[0]);
			$act  = $actb[1];
			if(method_exists($obj, $act)){
				$data = $obj->$act($acta);
			}
		}else if(method_exists($this->mListener, $act)){
			$data = $this->mListener->$act($acta);
		}else{
			$data = $this->strtoarray($dstr);
		}
		return $data;
	}
	
	
	/**
	*	创建表
	*/
	public function createTable($db)
	{
		$dbtype = $db->getDbtype();
		if($dbtype!= 'sqlite'){
			$idkey 	= '`id` int(11) NOT NULL AUTO_INCREMENT';
			$jian   = '';
			foreach($this->indexArray as $k=>$rs)if($rs['type']=='jian')$jian.=',`'.$rs['fields'].'`';
			$okey	= ',PRIMARY KEY (`id`'.$jian.')';
			foreach($this->indexArray as $k=>$rs){
				if($rs['type']=='key')$okey.=',KEY '.$rs['name'].' ('.$rs['fields'].')';
				if($rs['type']=='only')$okey.=',UNIQUE KEY '.$rs['name'].' ('.$rs['fields'].')';
			}
		}else{
			$idkey	= '`id` INTEGER PRIMARY KEY AUTOINCREMENT';
			$okey 	= '';
		}
		foreach($this->fieldsArray as $k=>$rs){
			if($rs['isfields']==0 || $rs['fields']=='id')continue;
			$str = DB::fieldsString($rs, $dbtype);
			$idkey .= ",\n".$str."";
		}
		$sql = "CREATE TABLE IF NOT EXISTS `{$this->tablename}` (".$idkey."".$okey."\n)";
		if($dbtype!= 'sqlite')$sql.= "ENGINE=InnoDB DEFAULT CHARSET=".$db->getDbencode()." COMMENT='{$this->name}'";
		$db->query($sql.';');
		if($dbtype == 'sqlite')foreach($this->indexArray as $k=>$rs){
			$sql = '';
			if($rs['type']=='key')$sql = "CREATE INDEX ".$rs['name']." on `{$this->tablename}` (".$rs['fields'].")";
			if($rs['type']=='only' || $rs['type']=='jian')$sql = "CREATE UNIQUE INDEX ".$rs['name']." on `{$this->tablename}` (".$rs['fields'].")";
			if($sql)$db->query($sql);
		}
		CLog::show('-createdb('.$this->tablename.')success-');
	}
	
	/**
	*	更新表
	*/
	public function updateTable($db, $alltable)
	{
		$this->nDb 			= $db;
		$dbtype 			= $db->getDbtype();
		$this->tablename    = $db->getDbperfix($this->table);
		if(!in_array($this->tablename, $alltable)){
			$this->createTable($db);
		}else{
			$allfields 	= $db->getAllFields($this->tablename);
			$farr		= array();
			foreach($allfields as $k=>$rs)$farr[$rs['fields']] = $rs;
			$usql		= array();
			foreach($this->fieldsArray as $k=>$rs){
				$fid  = $rs['fields'];
				if($rs['isfields']==0 || $fid=='id')continue;
				$nstr = DB::fieldsString($rs, $dbtype);
				if(!isset($farr[$fid])){
					$usql[]  = 'ADD COLUMN '.$nstr.'';
				}else if($dbtype != 'sqlite'){
					$ors 	= $farr[$fid]; $ors['name'] = '';
					$ostr 	= DB::fieldsString($ors, $dbtype);
					$ux 	= stripos($nstr, $ostr);
					if($ors['type']==$rs['type'] && $ors['len'] > 0 && $ors['len'] > $rs['len'])$ux = 1;
					if(is_bool($ux) && !$ux)$usql[] = 'MODIFY COLUMN '.$nstr.'';
				}
			}
			if($usql){
				if($dbtype != 'sqlite'){
					$usql = join(',', $usql);
					$db->query('ALTER TABLE `'.$this->tablename.'` '.$usql.';');
					CLog::show($usql);
				}else{
					foreach($usql as $sql1){
						$db->query('ALTER TABLE `'.$this->tablename.'` '.$sql1.';');
						CLog::show($sql1);
					}
				}
				CLog::show('-updatedb('.$this->tablename.')success-');
			}
		}
		$this->model	= $this->getModel($db);
		$this->initFirst();
		if($db->isError)CLog::error($db->errorall());
	}
}